package com.rdc.qingkou.view.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.ui.BrowseActivity;
import com.rdc.qingkou.ui.OtherHomepageActivity;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.vo.CardAndUserVO;
import com.rdc.qingkou.vo.CardVO;
import com.rdc.qingkou.vo.UserVO;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;
import de.hdodenhof.circleimageview.CircleImageView;

public class BrowseFragment extends BaseMediaPlayerFragment {


    private BrowseActivity mActivity;
    private ImageView mSubscribeImageView;
    private CardVO card;
    private UserVO user;
    private JZVideoPlayerStandard player;
    private CardAndUserVO cardAndUserVO;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            cardAndUserVO = (CardAndUserVO) savedInstanceState.getSerializable("cardAndUserVO");
            if (cardAndUserVO != null) {
                card = cardAndUserVO.getCardVO();
                user = cardAndUserVO.getUserVO();
            }
        }
        View view = inflater.inflate(R.layout.item_browse_card, container, false);
        ImageView cardImage = (ImageView) view.findViewById(R.id.layout_card_ImageView);
        ImageView voice = (ImageView) view.findViewById(R.id.layout_card_voice_imageView);
        TextView content = (TextView) view.findViewById(R.id.layout_card_content_textView);
        CircleImageView headImage = (CircleImageView) view.findViewById(R.id.item_browse_head_circleImageView);
        mSubscribeImageView = (ImageView) view.findViewById(R.id.item_browse_subscribe_imageView);
        TextView signature = (TextView) view.findViewById(R.id.item_browse_signature_textView);
        TextView name = (TextView) view.findViewById(R.id.item_browse_author_textView);
        if (card.getMediaUrl() != null) {
            //有视频,则不显示图片和音频
            cardImage.setVisibility(View.GONE);
            voice.setVisibility(View.GONE);
            player = (JZVideoPlayerStandard) view.findViewById(R.id.layout_card_videoPlayer);
            player.setVisibility(View.VISIBLE);
            player.setUp(card.getMediaUrl(), JZVideoPlayer.SCREEN_LAYOUT_LIST, "");
            //设置封面
            //mPlayer.thumbImageView.setImageBitmap(MyUtils.getNetVideoBitmap(URL.HEAD_YUN + mCard.getMediaUrl()));
        } else if (card.getImageUrl() != null) {
            Glide.with(this).load(card.getImageUrl()).placeholder(R.drawable.imageview_default).into(cardImage);
        }
        if (card.getVoiceUrl() != null) {
            voiceUrl = card.getVoiceUrl();
            voice.setOnClickListener(this);
        } else {
            voice.setVisibility(View.GONE);
        }
        content.setText(card.getContent());
        Glide.with(getContext()).load(user.getHeadImageUrl()).into(headImage);
        headImage.setOnClickListener(this);
        if (user.getNickName() != null){
            name.setText(user.getNickName().replace("\"",""));
        }
        if (user.getSignature() != null){
            signature.setText(user.getSignature().replace("\"",""));
        }
        if (user.getUserId() != MyApplication.getUser().getUserId()) {
            mSubscribeImageView.setOnClickListener(this);
            if (user.getSubsribeStatus()) {
                mSubscribeImageView.setImageResource(R.drawable.button_subscribe_pressure);
            } else {
                mSubscribeImageView.setImageResource(R.drawable.button_subscribe_normal);
            }
        } else {
            mSubscribeImageView.setImageResource(R.drawable.button_subscribe_normal);
        }
        View card = view.findViewById(R.id.item_browse_card_layout);
        card.setOnClickListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (BrowseActivity) getActivity();
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_browse_subscribe_imageView:
                if (card.getCardId() == MyApplication.getUser().getUserId()) {
                    Toast.makeText(mActivity, "不可以订阅自己哦！", Toast.LENGTH_SHORT).show();
                    break;
                }
                if (MyUtils.isNetworkConnected(mActivity)) {
                    if (user.getSubsribeStatus()) {
                        //取消订阅
                        AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
                        dialog.setMessage("确认不再关注此人?");
                        dialog.setCancelable(true);
                        dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mSubscribeImageView.setImageResource(R.drawable.button_subscribe_normal);
                                mActivity.subscribe(user.getUserId(), MyApplication.getUser().getUserId());
                                user.setSubsribeStatus(!user.getSubsribeStatus());
                            }
                        });
                        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        dialog.show();
                    } else {
                        //订阅
                        mActivity.subscribe(user.getUserId(), MyApplication.getUser().getUserId());
                        user.setSubsribeStatus(!user.getSubsribeStatus());
                        mSubscribeImageView.setImageResource(R.drawable.button_subscribe_pressure);
                    }
                } else {
                    Toast.makeText(mActivity, "网络不可用！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.item_browse_card_layout:
                mActivity.enterCardDetail(card);
                break;
            case R.id.item_browse_head_circleImageView:
                Intent intent = new Intent(mActivity, OtherHomepageActivity.class);
                intent.putExtra("user", user);
                startActivity(intent);
                break;

        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("cardAndUserVO", cardAndUserVO);
    }

    public void setCard(CardAndUserVO c) {
        card = c.getCardVO();
        user = c.getUserVO();
        cardAndUserVO = c;
    }

    public CardAndUserVO getCard() {
        return cardAndUserVO;
    }


}
